#include "data_structure_manager.h"

Facility* facilities[MAX_FACILITIES] = { NULL };

void Manager_Change_facility_code(FacilityManager i) {

}

void Menu(System* sys) {
    //print_facility_managers(&sys->facility_manager_link); //1
    printf("*******************************************\n");
    printf("*           欢迎使用管理员系统            *\n");
    printf("*******************************************\n");
    printf("*           请选择您的使用功能            *\n");
    printf("*                b,信息管理               *\n");
    printf("*                c,信息查询               *\n");
    printf("*                d,信息排序               *\n");
    printf("*                e,信息统计               *\n");
    printf("*                f,密码维护               *\n");
    printf("*                a,退出                   *\n");
    printf("*******************************************\n");
    char a;
    scanf_s("%c", &a);
    clear_input_buffer2();
    switch (a)
    {
    case 'b':
        system("cls");
        Data_Manage(sys);
        break;
    case 'c':
        system("cls");
        look_for(*sys);
        char ch4;
        while (true) {
            printf(" 请选择：");
            ch4 = getchar();
            clear_input_buffer();
            if (ch4 == '1' || ch4 == '2' || ch4 == '3' || ch4 == '4' || ch4 == 'q' || ch4 == 'b')  break;
            else {
                printf(" 输入有误，请重新输入\n");
            }
        }
        switch (ch4) {
        case'1':    //查询预订信息
            system("cls");
            look_for_res(*sys);
            char cha1;
            while (true) {
                printf(" 请选择：");
                cha1 = getchar();
                clear_input_buffer();
                if (cha1 == '1' || cha1 == '2' || cha1 == '3' || cha1 == '4' || cha1 == '5' || cha1 == 'b' || cha1 == 'q')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (cha1) {
            case'1':    // 查询预订信息（姓名 模糊）
                search_reservation_by_name(*sys);
                system("pause");
                break;
            case'2':    // 查询预订信息（电话）
                search_reservation_by_phone(*sys);
                system("pause");
                break;
            case'3':    // 查询预订信息（日期）
                search_reservation_by_date(*sys);
                system("pause");
                break;
            case'4':    // 查询预订信息（姓名+日期）
                search_reservation_by_name_date(*sys);
                system("pause");
                break;
            case'5':    // 查询预订信息（电话+日期）
                search_reservation_by_phone_date(*sys);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
            }
            break;
        case'2':    //查询使用记录信息 
            system("cls");
            look_for_record(*sys);
            char cha2;
            while (true) {
                printf(" 请选择：");
                cha2 = getchar();
                clear_input_buffer();
                if (cha2 == '1' || cha2 == '2' || cha2 == '3' || cha2 == '4' || cha2 == '5' || cha2 == 'b' || cha2 == 'q')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (cha2) {
            case'1':    // 查询使用记录 用户电话
                search_record_by_phone(*sys);
                system("pause");
                break;
            case'2':    // 查询使用记录 场地ID
                search_record_by_facility_id(*sys);
                system("pause");
                break;
            case'3':    // 查询使用记录 日期
                search_record_by_date(*sys);
                system("pause");
                break;
            case'4':    // 查询预订信息（姓名+日期）
                search_record_by_phone_date(*sys);
                system("pause");
                break;
            case'5':    // 查询预订信息（电话+日期）
                search_record_by_facility_id_date(*sys);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
            }
            break;
        case'3':    //查找场地信息
            system("cls");
            look_for_facility(*sys);
            break;
        case'4':    //查找用户信息
            system("cls");
            look_for_user(*sys);
            char cha4;
            while (true) {
                printf(" 请选择：");
                cha4 = getchar();
                clear_input_buffer();
                if (cha4 == '1' || cha4 == '2' || cha4 == '3' || cha4 == '4' || cha4 == 'b' || cha4 == 'q')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (cha4) {
            case'1':    // 查询用户信息 昵称 模糊
                search_user_by_nickname(*sys);
                system("pause");
                break;
            case'2':    // 查询用户信息 电话
                search_user_by_phone(*sys);
                system("pause");
                break;
            case'3':    // 查询用户信息 身份证号
                search_user_by_id_num(*sys);
                system("pause");
                break;
            case'4':    // 查询用户信息 真实姓名 模糊
                search_user_by_name(*sys);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
            }
            break;
        case'b':
            break;
        case'q':
            exit(0);
        }
        //data_check(sys);
        break;
    case 'd':
        system("cls");
        my_sort(*sys);
        char sort5;
        while (true) {
            printf(" 请选择：");
            sort5 = getchar();
            clear_input_buffer();
            if (sort5 == '1' || sort5 == '2' || sort5 == '3' || sort5 == '4' || sort5 == 'q' ||
                sort5 == '5' || sort5 == '6' || sort5 == '7' || sort5 == 'b')  break;
            else {
                printf(" 输入有误，请重新输入\n");
            }
        }
        switch (sort5) {
        case'1':    //  查看预订消息（单项排序）
            system("cls");
            sort_reservation();
            char s1;
            while (true) {
                printf(" 请选择：");
                s1 = getchar();
                clear_input_buffer();
                if (s1 == '1' || s1 == '2' || s1 == '3' || s1 == '4' || s1 == 'q' || s1 == 'b')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (s1) {
            case'1':    //Order by date
                sort_res_order_by_date(&sys->reservation_link);
                printf_res(sys->reservation_link);
                system("pause");
                break;
            case'2':    //Order by id
                sort_res_order_by_id(&sys->reservation_link);
                printf_res(sys->reservation_link);
                system("pause");
                break;
            case'3':    //Order by facility_id
                sort_res_order_by_facility_id(&sys->reservation_link);
                printf_res(sys->reservation_link);
                system("pause");
                break;
            case'4':    //Order by name
                sort_res_order_by_name(&sys->reservation_link);
                printf_res(sys->reservation_link);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'2':    //  查看预订消息（组合排序）
            system("cls");
            sort_res();
            int arr2[5];
            while (true) {
                printf("请输入组合数目：");
                scanf_s("%d", &arr2[0]);
                clear_input_buffer();
                if (arr2[0] < 1 && arr2[0]>4) {
                    printf("输入不合法\n");
                }
                else {
                    break;
                }
            }
            for (int i = 0; i < arr2[0]; i++) {
                while (true) {
                    printf("请依次输入组合：");
                    scanf_s("%d", &arr2[i + 1]);
                    clear_input_buffer();
                    if (arr2[i + 1] >= 1 && arr2[i + 1] <= 4) {
                        break;
                    }
                    else {
                        printf("输入不合法\n");
                    }
                }
            }
            for (int i = 0; i < arr2[0]; i++) {
                switch (arr2[arr2[0] - i]) {
                case 1:
                    sort_res_order_by_date(&sys->reservation_link);
                    break;
                case 2:
                    sort_res_order_by_id(&sys->reservation_link);
                    break;
                case 3:
                    sort_res_order_by_facility_id(&sys->reservation_link);
                    break;
                case 4:
                    sort_res_order_by_name(&sys->reservation_link);
                    break;
                }

            }
            printf_res(sys->reservation_link);
            system("pause");
            break;
        case'3':    //  查看使用记录（单项排序）
            system("cls");
            sort_record();
            //print_records(&sys->record_link);
            char s3;
            while (true) {
                printf(" 请选择：");
                s3 = getchar();
                clear_input_buffer();
                if (s3 == '1' || s3 == '2' || s3 == '3' || s3 == '4' || s3 == 'q' || s3 == 'b')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (s3) {
            case'1':    //Order by date
                sort_record_order_by_date(&sys->record_link);
                print_records(&sys->record_link);
                system("pause");
                break;
            case'2':    //Order by id
                sort_record_order_by_id(&sys->record_link);
                print_records(&sys->record_link);
                system("pause");
                break;
            case'3':    //Order by facility_id
                sort_record_order_by_facility_id(&sys->record_link);
                print_records(&sys->record_link);
                system("pause");
                break;
            case'4':    //Order by name
                sort_record_order_by_name(&sys->record_link);
                print_records(&sys->record_link);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'4':    //  查看使用记录（组合排序）
            system("cls");
            sort_records();
            int arr4[5];
            while (true) {
                printf("请输入组合数目：");
                scanf_s("%d", &arr4[0]);
                clear_input_buffer();
                if (arr4[0] < 1 && arr4[0]>4) {
                    printf("输入不合法\n");
                }
                else {
                    break;
                }
            }
            for (int i = 0; i < arr4[0]; i++) {
                while (true) {
                    printf("请依次输入组合：");
                    scanf_s("%d", &arr4[i + 1]);
                    clear_input_buffer();
                    if (arr4[i + 1] >= 1 && arr4[i + 1] <= 4) {
                        break;
                    }
                    else {
                        printf("输入不合法\n");
                    }
                }
            }
            for (int i = 0; i < arr4[0]; i++) {
                switch (arr4[arr4[0] - i]) {
                case 1:
                    sort_record_order_by_date(&sys->record_link);
                    break;
                case 2:
                    sort_record_order_by_id(&sys->record_link);
                    break;
                case 3:
                    sort_record_order_by_facility_id(&sys->record_link);
                    break;
                case 4:
                    sort_record_order_by_name(&sys->record_link);
                    break;
                }

            }
            print_records(&sys->record_link);
            system("pause");
            break;
        case'5':    //  查看场地（单项排序）
            system("cls");
            sort_facility();
            char s5;
            while (true) {
                printf(" 请选择：");
                s5 = getchar();
                clear_input_buffer();
                if (s5 == '1' || s5 == '2' || s5 == '3' || s5 == '4' || s5 == 'q' || s5 == 'b')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (s5) {
            case'1':    //Order by id
                sort_facility_order_by_id(&sys->facility_link);
                print_facilities(&sys->facility_link);
                system("pause");
                break;
            case'2':    //Order by area
                sort_facility_order_by_area(&sys->facility_link);
                print_facilities(&sys->facility_link);
                system("pause");
                break;
            case'3':    //Order by type
                sort_facility_order_by_type(&sys->facility_link);
                print_facilities(&sys->facility_link);
                system("pause");
                break;
            case'4':    //Order by fee
                sort_facility_order_by_fee(&sys->facility_link);
                print_facilities(&sys->facility_link);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'6':    //  查看场地（组合排序）
            system("cls");
            sort_fac();
            int arr6[5];
            while (true) {
                printf("请输入组合数目：");
                scanf_s("%d", &arr6[0]);
                clear_input_buffer();
                if (arr6[0] < 1 && arr6[0]>4) {
                    printf("输入不合法\n");
                }
                else {
                    break;
                }
            }
            for (int i = 0; i < arr6[0]; i++) {
                while (true) {
                    printf("请依次输入组合：");
                    scanf_s("%d", &arr6[i + 1]);
                    clear_input_buffer();
                    if (arr6[i + 1] >= 1 && arr6[i + 1] <= 4) {
                        break;
                    }
                    else {
                        printf("输入不合法\n");
                    }
                }
            }
            for (int i = 0; i < arr6[0]; i++) {
                switch (arr6[arr6[0] - i]) {
                case 1:
                    sort_facility_order_by_id(&sys->facility_link);
                    break;
                case 2:
                    sort_facility_order_by_area(&sys->facility_link);
                    break;
                case 3:
                    sort_facility_order_by_type(&sys->facility_link);
                    break;
                case 4:
                    sort_facility_order_by_fee(&sys->facility_link);
                    break;
                }

            }
            print_facilities(&sys->facility_link);
            system("pause");
            break;
            break;
        case'7':    //  查看用户（单项排序）
            system("cls");
            sort_user();
            char s7;
            while (true) {
                printf(" 请选择：");
                s7 = getchar();
                clear_input_buffer();
                if (s7 == '1' || s7 == '2' || s7 == '3' || s7 == 'q' || s7 == 'b')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (s7) {
            case'1':    //Order by name
                sort_user_order_by_name(&sys->user_link);
                printf_user(sys->user_link);
                system("pause");
                break;
            case'2':    //Order by phone
                sort_user_order_by_phone(&sys->user_link);
                printf_user(sys->user_link);
                system("pause");
                break;
            case'3':    //Order by name + phone
                sort_user_order_by_name(&sys->user_link);
                sort_user_order_by_phone(&sys->user_link);
                printf_user(sys->user_link);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'b':
            break;
        case'q':
            exit(0);
            break;
        }
        //data_sort();
        break;
    case 'e':
        //data_count();
        system("cls");
        statistic(*sys);
        char ch6;
        char statistic6;
        while (true) {
            printf(" 请选择：");
            statistic6 = getchar();
            clear_input_buffer();
            if (statistic6 == '1' || statistic6 == '2' || statistic6 == '3' || statistic6 == '4' ||
                statistic6 == '5' || statistic6 == '6' || statistic6 == 'b' || statistic6 == 'q')  break;
            else {
                printf(" 输入有误，请重新输入\n");
            }
        }
        switch (statistic6) {
        case'1':    // 预订消息统计
            system("cls");
            reservation_statistic(*sys);
            char sta1;
            while (true) {
                printf(" 请选择：");
                sta1 = getchar();
                clear_input_buffer();
                if (sta1 == '1' || sta1 == '2' || sta1 == '3' || sta1 == '4' || sta1 == 'b' || sta1 == 'q')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (sta1) {
            case'1':
                statistic_res_by_user(*sys);
                system("pause");
                break;
            case'2':
                statistic_res_by_facility_type(*sys);
                system("pause");
                break;
            case'3':
                statistic_res_by_user_and_facility_type(*sys);
                system("pause");
                break;
            case'4':
                statistic_res_by_date_range(*sys);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'2':    // 使用记录统计
            system("cls");
            record_statistic(*sys);
            char sta2;
            while (true) {
                printf(" 请选择：");
                sta2 = getchar();
                clear_input_buffer();
                if (sta2 == '1' || sta2 == '2' || sta2 == '3' || sta2 == '4' ||
                    sta2 == '5' || sta2 == 'b' || sta2 == 'q')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (sta2) {
            case'1':    //根据用户
                statistic_record_by_user(*sys);
                system("pause");
                break;
            case'2':    //根据场地
                statistic_record_by_facility(*sys);
                system("pause");
                break;
            case'3':    //根据日期
                statistic_record_by_date(*sys);
                system("pause");
                break;
            case'4':    //根据用户+场地
                statistic_record_by_user_facility(*sys);
                system("pause");
                break;
            case'5':    //根据日期范围
                statistic_record_by_date_range(*sys);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'3':    // 场地信息统计
            system("cls");
            facility_statistic(*sys);
            char sta3;
            while (true) {
                printf(" 请选择：");
                sta3 = getchar();
                clear_input_buffer();
                if (sta3 == '1' || sta3 == '2' || sta3 == '3' || sta3 == '4' || sta3 == '5' ||
                    sta3 == '6' || sta3 == '7' || sta3 == 'b' || sta3 == 'q')  break;
                else {
                    printf(" 输入有误，请重新输入\n");
                }
            }
            switch (sta3) {
            case'1':    //统计场地 根据类型
                statistic_facility_by_type(*sys);
                system("pause");
                break;
            case'2':    //统计场地 根据价格
                statistic_facility_by_price(*sys);
                system("pause");
                break;
            case'3':    //统计场地 根据面积
                statistic_facility_by_area(*sys);
                system("pause");
                break;
            case'4':    //统计场地 根据类型+价格
                statistic_facility_by_type_price(*sys);
                system("pause");
                break;
            case'5':    //统计场地 根据类型+面积
                statistic_facility_by_type_area(*sys);
                system("pause");
                break;
            case'6':    //统计场地 根据面积范围
                statistic_facility_by_area_range(*sys);
                system("pause");
                break;
            case'7':    //统计场地 根据价格范围
                statistic_facility_by_price_range(*sys);
                system("pause");
                break;
            case'b':
                break;
            case'q':
                exit(0);
                break;
            }
            break;
        case'4':    // 预约信息总统计
            res_all_statistic(*sys);
            system("pause");
            break;
        case'5':    // 使用记录总统计
            statistic_all_record(*sys);
            system("pause");
            break;
        case'6':    // 未到场的预约消息统计
            res_default_statistic(*sys);
            system("pause");
            break;
        case'b':
            break;
        case'q':
            exit(0);
            break;
        }
        break;
    case 'f':
        System_defend(sys);
        break;
    case 'a':
        break;
    default:
        printf("输入错误请重试\n");
        Data_Manage(sys);   //???
        break;
    }
    //clear_input_buffer3();
}

void Data_Manage(System* sys) {
    printf("*******************************************\n");
    printf("*           欢迎使用管理员系统            *\n");
    printf("*******************************************\n");
    printf("*           请选择您的使用功能            *\n");
    printf("*                a,场地负责人信息管理     *\n");
    printf("*                b,场地信息管理           *\n");
    printf("*                c,返回                   *\n");
    printf("*******************************************\n");

    char i;
    scanf_s("%c", &i); clear_input_buffer3();
    switch (i) {
    case 'a':
        system("cls");
        FacilityManager_datamanage(sys);
        break;

    case 'b':
        system("cls");
        Facility_datamanage(sys);
        break;

    default:
        printf("输入错误请重试\n");
        system("pause");
        system("cls");
        Data_Manage(sys);
        break;
    case 'c':
        system("cls");
        Menu(sys);
        break;
    }
    
}

void FacilityManager_datamanage(System* sys) {
    printf("*******************************************\n");
    printf("*           欢迎使用管理员系统            *\n");
    printf("*******************************************\n");
    printf("*           请选择您的使用功能            *\n");
    printf("*                a,增加                   *\n");
    printf("*                b,删除                   *\n");
    printf("*                c,修改                   *\n");
    printf("*                d,返回                   *\n");
    printf("*******************************************\n");

    char i;
    scanf_s("%c", &i);
    clear_input_buffer2();
    switch (i) {
    case 'a':
    {
        int id; char name[10]; int num_facilities; char username[10]; char password[20]; long long phone; 
        printf("请输入id:"); scanf_s("%d", &id); clear_input_buffer3();
        printf("请输入姓名:"); scanf_s("%s", name,10); clear_input_buffer3();
        printf("场地数目默认为0\n");
        printf("请输入用户名:"); scanf_s("%s", username,10); clear_input_buffer3();
        printf("请输入密码:"); scanf_s("%s", password,20); clear_input_buffer3();
        printf("请输入手机号："); scanf_s("%lld", &phone); clear_input_buffer3();
        FacilityManager new_facility_manager = create_facility_manager(id, name, 0, facilities, username, password, phone);
        add_facility_manager(&sys->facility_manager_link, &new_facility_manager);
        write_facility_manager_file(*sys, "..\\src\\facility_manager.txt");
        printf("操作成功\n");
        system("pause");
        system("cls");
        FacilityManager_datamanage(sys);
    }
    break;
    case 'b':
    {
        int c;
        printf("请输入删除人id:");
        scanf_s("%d", &c);
        clear_input_buffer2();
        delete_facility_manager(&sys->facility_manager_link, c);
        write_facility_manager_file(*sys, "..\\src\\facility_manager.txt");
        printf("完成操作！\n");
        system("pause");
        system("cls");
        FacilityManager_datamanage(sys);
    }

    break;
    case 'c':
        ////str i;
        ////scanf_s("%s",&i);
        //// 错误
        //Change_facility_manager(i);
        //FacilityManager_datamanage(sys);
    {
        /*int id;
        printf("请输入更改人id:");
        scanf_s("%d", &id);
        FacilityManager* fm = find_facility_manager(&sys->facility_manager_link, id);*/
        Change_facility_manager(sys); 
        printf("操作成功\n");
        system("pause");
        system("cls");
        FacilityManager_datamanage(sys);
    }

    break;
    default:
        printf("输入错误请重试\n");
        system("pause");
        system("cls");
        FacilityManager_datamanage(sys);
        break;
    case 'd':

        system("cls");
        Data_Manage(sys);
        break;
    }
    //print_facility_managers(&sys->facility_manager_link);
    //clear_input_buffer3();

}
void Change_facility_manager(System* sys) {
    system("cls");
    print_facility_managers(&sys->facility_manager_link);
    printf("请选择更改场地管理员id:");
    int id; scanf_s("%d", &id); clear_input_buffer3();
    FacilityManager* fm = find_facility_manager(&sys->facility_manager_link, id);
    printf("请选择修改项目\n");
    Change_fm_datailed(fm, sys);
}

void Change_fm_datailed(FacilityManager* fm, System* sys) {
    char i;
    printf("*******************************************\n");
    printf("*           欢迎使用管理员系统            *\n");
    printf("*******************************************\n");
    printf("*           请选择您的使用功能            *\n");
    printf("*                a, 场地负责人ID          *\n");
    printf("*                b, 场地负责人姓名        *\n");
    printf("*                c, 场地负责人联系方式    *\n");
    printf("*                d, 场地负责人密码        *\n");
    printf("*                e, 负责人管理的场地      *\n");
    printf("*                f, 返回                  *\n");
    printf("*******************************************\n");
    scanf_s("%c", &i); clear_input_buffer3();
    switch (i) {
    case 'a':
    {
        int nid;
        printf("请输入新id:");
        scanf_s("%d", &nid); clear_input_buffer3();
        FacilityManager changed_facility_manager = create_facility_manager(nid, fm->name, fm->num_facilities, fm->facilities, fm->username, fm->password, fm->phone);
        add_facility_manager(&sys->facility_manager_link, &changed_facility_manager);
        delete_facility_manager(&sys->facility_manager_link, fm->id);
        write_facility_manager_file(*sys, "..\\src\\facility_manager.txt");
        Change_fm_datailed(&changed_facility_manager, sys);
    }
    break;

    case 'b':
    {
        char nname[MAX_NAME_LENGTH];
        printf("请输入新姓名:");
        scanf_s("%s", nname, MAX_NAME_LENGTH); clear_input_buffer3();
        FacilityManager changed_facility_manager = create_facility_manager(fm->id, nname, fm->num_facilities, fm->facilities, fm->username, fm->password, fm->phone);
        add_facility_manager(&sys->facility_manager_link, &changed_facility_manager);
        delete_facility_manager(&sys->facility_manager_link, fm->id);
        write_facility_manager_file(*sys, "..\\src\\facility_manager.txt");
        Change_fm_datailed(&changed_facility_manager, sys);
    }
    break;
    case 'c':
    {
        long long nphone;
        printf("请输入新手机号:");
        scanf_s("%lld", &nphone); clear_input_buffer3();
        FacilityManager changed_facility_manager = create_facility_manager(fm->id, fm->name, fm->num_facilities, fm->facilities, fm->username, fm->password, nphone);
        add_facility_manager(&sys->facility_manager_link, &changed_facility_manager);
        delete_facility_manager(&sys->facility_manager_link, fm->id);
        write_facility_manager_file(*sys, "..\\src\\facility_manager.txt");
        Change_fm_datailed(&changed_facility_manager, sys);
    }
    break;

    case 'd':
        printf("请输入新密码:");
        char ncode[MAX_PASSWORD];
        scanf_s("%s", ncode, MAX_NAME_LENGTH); clear_input_buffer3();
        FacilityManager changed_facility_manager = create_facility_manager(fm->id, fm->name, fm->num_facilities, fm->facilities, fm->username, ncode, fm->phone);
        add_facility_manager(&sys->facility_manager_link, &changed_facility_manager);
        delete_facility_manager(&sys->facility_manager_link, fm->id);
        write_facility_manager_file(*sys, "..\\src\\facility_manager.txt");
        Change_fm_datailed(&changed_facility_manager, sys);
        break;

    case 'e':
        if (fm->num_facilities == 0) {
            printf("请输入增添设备数:");
            int nnum_facility;
            scanf_s("%d", &nnum_facility); clear_input_buffer3();
            while (nnum_facility) {
                Facility target1 = create_facility(*sys, 0, "0", 0, 0, 0, "0", 0, 0);
                Facility* target = &target1;
                printf("\n请选择要增添的项目：");
                printf("\n1. 场地名称");
                char new_name[MAX_NAME_LENGTH];
                printf("请输入新名称（最多%d字符）: ", MAX_NAME_LENGTH - 1);
                fgets(new_name, MAX_NAME_LENGTH, stdin);
                new_name[strcspn(new_name, "\n")] = '\0';  // 去除换行符
                strncpy_s(target->name, MAX_NAME_LENGTH, new_name, _TRUNCATE);

                printf("\n2. 场地类型");
                printf("\n可选类型：");
                printf("\n1-篮球场 2-羽毛球场 3-足球场");
                printf("\n4-台球场 5-乒乓球场 6-排球场");
                printf("\n请输入新类型编号：");

                int type;
                scanf_s("%d", &type);
                clear_input_buffer2();

                if (type < 1 || type > 6) {
                    printf("无效类型编号\n");
                    return;
                }
                target->type = (VenueType)type;

                printf("\n3. 场地面积");
                printf("请输入新面积（平方米）: ");
                int area;
                scanf_s("%d", &area);
                clear_input_buffer2();
                target->area = area > 0 ? area : target->area;

                printf("\n4. 开放时间");
                char new_time[MAX_NAME_LENGTH];
                printf("请输入新开放时间（格式示例：08:00-22:00）: ");
                fgets(new_time, MAX_NAME_LENGTH, stdin);
                new_time[strcspn(new_time, "\n")] = '\0';
                strncpy_s(target->open_time, MAX_NAME_LENGTH, new_time, _TRUNCATE);

                printf("\n5. 价格/半小时");
                printf("请输入新价格（元/半小时）: ");
                float price;
                scanf_s("%f", &price);
                clear_input_buffer2();
                target->price_per_half_hour = price > 0 ? price : target->price_per_half_hour;

                printf("\n6. 最大容纳人数");
                printf("请输入新最大容纳人数: ");
                int max_num;
                scanf_s("%d", &max_num);
                clear_input_buffer2();
                target->max_person_num = max_num > 0 ? max_num : target->max_person_num;

                printf("\n0. 添加完成\n");
                nnum_facility--;
                
                add_facility(&sys->facility_link, target);
                write_facility_file(*sys, "..\\src\\facility.txt");
                fm->facilities[fm->num_facilities] = target;
                fm->num_facilities++;
                write_facility_manager_file(*sys, "..\\src\\facility_manager.txt");

            }

            
            
        }
        else
        {
            Change_facility(fm, sys);
        }
        FacilityManager_datamanage(sys);
         break;
    case 'f':
        system("cls");
        FacilityManager_datamanage(sys);
        break;
    default:
        printf("输入错误");
        Change_fm_datailed(fm, sys);
        break;
        }
    }

// 修改负责人管理的具体场地信息
void Change_facility(FacilityManager* fm, System* sys) {
    if (fm == NULL || fm->num_facilities == 0) {
        printf("该负责人暂无管理场地\n");
        return;
    }

    // 显示当前管理的所有场地
    printf("\n当前管理的场地列表\n");
    for (int i = 0; i < fm->num_facilities; i++) {
        Facility* f = fm->facilities[i];
        printf("[%d] ID:%d 名称:%s 类型:", i + 1, f->id, f->name);
        print_facility_type(f->type);
        printf("\n");
    }
    
    // 选择要修改的场地
    int index;
    printf("\n请选择要修改的场地序号(1-%d): ", fm->num_facilities);
    scanf_s("%d", &index);
    clear_input_buffer2();

    if (index < 1 || index > fm->num_facilities) {
        printf("输入序号无效\n");
        return;
    }

    Facility* target = fm->facilities[index - 1];

    // 显示修改菜单
    

    char choice;
    printf("请选择修改类型：\n");
    printf("\na. 场地名称");
    printf("\nb. 场地类型");
    printf("\nc. 场地面积");
    printf("\nd. 开放时间");
    printf("\ne. 价格/半小时");
    printf("\nf. 最大容纳人数");
    printf("\ng. 返回");
    printf("\n请输入选项：");
    scanf_s("%c", &choice);
    clear_input_buffer2();

    switch (choice) {
    case 'a': {  // 修改名称
        char new_name[MAX_NAME_LENGTH];
        printf("请输入新名称（最多%d字符）: ", MAX_NAME_LENGTH - 1);
        fgets(new_name, MAX_NAME_LENGTH, stdin);
        new_name[strcspn(new_name, "\n")] = '\0';  // 去除换行符
        strncpy_s(target->name, MAX_NAME_LENGTH, new_name, _TRUNCATE);
        break;
    }

    case 'b': {  // 修改类型
        printf("\n可选类型：");
        printf("\n1-篮球场 2-羽毛球场 3-足球场");
        printf("\n4-台球场 5-乒乓球场 6-排球场");
        printf("\n请输入新类型编号：");

        int type;
        scanf_s("%d", &type);
        clear_input_buffer2();

        if (type < 1 || type > 6) {
            printf("无效类型编号\n");
            return;
        }
        target->type = (VenueType)type;
        break;
    }

    case 'c': {  // 修改面积
        printf("请输入新面积（平方米）: ");
        int area;
        scanf_s("%d", &area);
        clear_input_buffer2();
        target->area = area > 0 ? area : target->area;
        break;
    }

    case 'd': {  // 修改开放时间
        char new_time[MAX_NAME_LENGTH];
        printf("请输入新开放时间（格式示例：08:00-22:00）: ");
        fgets(new_time, MAX_NAME_LENGTH, stdin);
        new_time[strcspn(new_time, "\n")] = '\0';
        strncpy_s(target->open_time, MAX_NAME_LENGTH, new_time, _TRUNCATE);
        break;
    }

    case 'e': {  // 修改价格
        printf("请输入新价格（元/半小时）: ");
        float price;
        scanf_s("%f", &price);
        clear_input_buffer2();
        target->price_per_half_hour = price > 0 ? price : target->price_per_half_hour;
        break;
    }

    case 'f': {  // 修改最大人数
        printf("请输入新最大容纳人数: ");
        int max_num;
        scanf_s("%d", &max_num);
        clear_input_buffer2();
        target->max_person_num = max_num > 0 ? max_num : target->max_person_num;
        break;
    }

    case 'g':
        return;

    default:
        printf("无效选项\n");
    }

    // 写入文件保存修改
    write_facility_file(*sys, "..\\src\\facility.txt");

    FacilityManager changed_facility_manager = create_facility_manager(fm->id, fm->name, fm->num_facilities, fm->facilities, fm->username, fm->password, fm->phone);
    add_facility_manager(&sys->facility_manager_link, &changed_facility_manager);
    delete_facility_manager(&sys->facility_manager_link, fm->id);

    printf("\n修改成功\n");
}

void Facility_datamanage(System* sys) {
    printf("*******************************************\n");
    printf("*           欢迎使用管理员系统            *\n");
    printf("*******************************************\n");
    printf("*           请选择您的使用功能            *\n");
    printf("*                a,增加                   *\n");
    printf("*                b,删除                   *\n");
    printf("*                c,修改                   *\n");
    printf("*                d,返回                   *\n");
    printf("*******************************************\n");

    char i;
    scanf_s("%c", &i); clear_input_buffer2();
    switch (i) {
    case 'a':
        {
            int id; char name[20]; int manager_id; int type; int area; char open_time[20]; float price; int max_person_num;
            printf("请输入场地编号:"); scanf_s("%d", &id); clear_input_buffer3();
            printf("请输入场地类型:"); scanf_s("%d", &type); clear_input_buffer3();
            printf("请输入场地面积:"); scanf_s("%d", &area); clear_input_buffer3();
            printf("请输入开放时间:"); scanf_s("%s", open_time,20); clear_input_buffer3();
            printf("请输入价格:"); scanf_s("%f", &price); clear_input_buffer3();
            printf("请输入最大容纳人数:"); scanf_s("%d", &max_person_num); clear_input_buffer3();
            Facility f = create_facility(*sys, id, "0", 0, type, area, open_time, price, max_person_num);
            add_facility(&sys->facility_link, &f);
            write_facility_file(*sys, "..\\src\\facility.txt");
            printf("添加成功！\n");
            system("pause");
            system("cls");
            Facility_datamanage(sys);
        }
        
        break;
    case 'b':
        {
            int b;
            printf("请输入删除场地id:");
            scanf_s("%d", &b); clear_input_buffer3();
            delete_facility(&sys->facility_link, b);
            write_facility_file(*sys, "..\\src\\facility.txt");
            printf("删除成功！\n");
            system("pause");
            system("cls");
            Facility_datamanage(sys);
        }
        break;

    case 'c': {
            int c;
            printf("请输入更改场地id:");
            scanf_s("%d", &c); clear_input_buffer3();
            print_facility_by_id(c, sys->facility_link);
            Facility* f1 = find_facility(&sys->facility_link,c);
            Change_f_datailed(f1, &sys->facility_link);
            Facility_datamanage(sys);
        }
        break;
    case 'd':// 缺少id
        {
           /* int facility_id;
            printf("请输入更改人id");
            scanf_s("%d", &facility_id);
            Facility* f = find_facility(&sys->facility_link, facility_id);
            printf("请选择修改项目");
            Change_f_datailed(&f, sys); */
            system("cls");
            Data_Manage(sys);
            break;
        }
    default:
        printf("输入错误\n");
        Facility_datamanage(sys);
        break;
    }
    //print_facility_managers(&sys->facility_manager_link);
}

void Change_f_datailed(Facility* f, System* sys) {
    printf("\n请选择要修改的项目：");
    printf("\na. 场地名称");
    printf("\nb. 场地类型");
    printf("\nc. 场地面积");
    printf("\nd. 开放时间");
    printf("\ne. 价格/半小时");
    printf("\nf. 最大容纳人数");
    printf("\ng. 返回");
    printf("\n请输入选项：");

    char c;
    scanf_s("%c", &c);
    clear_input_buffer3();

    switch (c) {
    case 'a': {  // 修改名称
        char new_name[MAX_NAME_LENGTH];
        printf("请输入新名称（最多%d字符）: ", MAX_NAME_LENGTH - 1);
        fgets(new_name, MAX_NAME_LENGTH, stdin);
        new_name[strcspn(new_name, "\n")] = '\0';  // 去除换行符
        strncpy_s(f->name, MAX_NAME_LENGTH, new_name, _TRUNCATE);
        break;
    }

    case 'b': {  // 修改类型
        printf("\n可选类型：");
        printf("\n1-篮球场 2-羽毛球场 3-足球场");
        printf("\n4-台球场 5-乒乓球场 6-排球场");
        printf("\n请输入新类型编号：");

        int type;
        scanf_s("%d", &type);
        clear_input_buffer3();

        if (type < 1 || type > 6) {
            printf("无效类型编号，请保持原值\n");
            return;
        }
        f->type = (VenueType)type;
        break;
    }

    case 'c': {  // 修改面积
        printf("请输入新面积（平方米）: ");
        int area;
        scanf_s("%d", &area);
        clear_input_buffer3();

        if (area <= 0) {
            printf("面积必须大于0，请保持原值\n");
            return;
        }
        f->area = area;
        break;
    }

    case 'd': {  // 修改开放时间
        char new_time[MAX_NAME_LENGTH];
        printf("请输入新开放时间（格式示例：08:00-22:00）: ");
        fgets(new_time, MAX_NAME_LENGTH, stdin);
        new_time[strcspn(new_time, "\n")] = '\0';
        strncpy_s(f->open_time, MAX_NAME_LENGTH, new_time, _TRUNCATE);
        break;
    }

    case 'e': {  // 修改价格
        printf("请输入新价格（元/半小时）: ");
        float price;
        scanf_s("%f", &price);
        clear_input_buffer3();

        if (price <= 0) {
            printf("价格必须大于0，请保持原值\n");
            return;
        }
        f->price_per_half_hour = price;
        break;
    }

    case 'f': {  // 修改最大人数
        printf("请输入新最大容纳人数: ");
        int max_num;
        scanf_s("%d", &max_num);
        clear_input_buffer3();

        if (max_num <= 0) {
            printf("人数必须大于0，请保持原值\n");
            return;
        }
        f->max_person_num = max_num;
        break;
    }

    case 'g': {  // 返回
        return;
    }

    default: {
        printf("无效选项，请重新输入\n");
        Change_f_datailed(f, sys);  // 递归调用以重新选择
        return;
    }
    }

    // ====== 保存修改到文件 ====== //
    // 缺少删除？
 
    write_facility_file(*sys, "..\\src\\facility.txt");
    printf("\n修改成功！\n");
    system("pause");
    system("cls");
}

void System_defend(System* sys) {
    //print_facility_managers(&sys->facility_manager_link); //1
    system("cls");
    printf("请输入选择功能\n");
    printf("*                a,用户密码维护               *\n");
    printf("*                b,管理员密码维护             *\n");
    printf("*                c,用户数据备份               *\n");
    printf("*                d,用户数据复原               *\n"); 
    printf("*                e,返回                       *\n");
    char i;
    scanf_s("%c", &i,20);
    clear_input_buffer2();
    switch (i) {
        case 'a':   // 用户密码维护
            printf("请输入用户姓名:");
            char name[20];
            scanf_s("%s", name,20);
            clear_input_buffer2();
            printf("请输入新密码:");
            char npassword[20];
            scanf_s("%s", npassword,20);
            clear_input_buffer2();
            maintain_user_password(&sys->user_link, name, npassword);
            system("pause");
            System_defend(sys);
            break;
        case 'b':
            //print_facility_managers(&sys->facility_manager_link); //2
            printf("请输入场地管理员id:");
            int id;
            scanf_s("%d", &id);
            clear_input_buffer2();
            printf("请输入新密码:");
            char newpassword[20];
            scanf_s("%s", newpassword,20);
            clear_input_buffer2();
            reset_venue_manager_password(sys,&sys->facility_manager_link, id, newpassword);
            system("pause");
            System_defend(sys);
            break;
        case 'c':
            backup_user_data(&sys->user_link);
            system("pause");
            System_defend(sys);
            break;
        case 'd':
            restore_user_data(&sys->user_link);
            system("pause");
            System_defend(sys);
            break;
        case 'e':
            system("cls");
            Menu(sys);
            break;
        default:
            printf("输入错误请重试");
            system("pause");
            System_defend(sys);
            break;

    }


}
// 用户密码维护函数
void maintain_user_password(User_list* user_list, const char* name, const char* new_password) {
    User_node* cur_node = user_list->head;

    while (cur_node != NULL) {
        if (strcmp(cur_node->user.name, name) == 0) { // 找到用户
            strcpy_s(cur_node->user.secret_code,sizeof(cur_node->user.secret_code), new_password); 
            save_uers_data(user_list);// 更新密码
            printf("密码修改成功！\n");
            return;
        }
        cur_node = cur_node->next;
    }

    printf("未找到用户 ID 为 %s 的用户！\n", name);
}



// 场地负责人密码重置函数
void reset_venue_manager_password(System *sys,FMList* fmlist,  const int manager_id, const char* new_password) {
    

    // 找到场地负责人并重置密码
    FMNode *cur_node = fmlist->head->next;
    //print_facility_managers(fmlist); //3
    while (cur_node != NULL) {
        if (cur_node->facility_manager.id==manager_id) { // 找到场地负责人
            strcpy_s(cur_node->facility_manager.password,20,new_password); // 更新密码
            write_facility_manager_file(*sys, "..\\src\\facility_manager.txt");
            printf("场地负责人密码重置成功！\n");
            return;
        }
        cur_node = cur_node->next;
    }

    
    printf("未找到场地负责人 ID 为 %d 的用户！\n", manager_id);
}

// 数据备份函数
void backup_user_data(User_list* user_list) {
    FILE* fp = fopen("..\\src\\users_data_backup.txt", "w");
    if (!fp) {
        perror("文件打开失败\n");
        return;
    }

    User_node* cur_node = user_list->head;
    while (cur_node != NULL) {
        fprintf(fp, "%s\t%s\t%s\t%s\t%s\n",
            cur_node->user.id_nicknames,
            cur_node->user.phone,
            cur_node->user.secret_code,
            cur_node->user.name,
            cur_node->user.id_name);
        cur_node = cur_node->next;
    }

    fclose(fp);
    printf("数据备份成功！备份文件：users_data_backup.txt\n");
}

// 数据恢复函数
void restore_user_data(User_list* user_list) {
    FILE* fp = fopen("..\\src\\users_data_backup.txt", "r");
    if (!fp) {
        perror("文件打开失败\n");
        return;
    }

    // 清空现有链表
    free_list(user_list);

    // 从文件中读取数据
    char id_nicknames[50], phone[50], secret_code[50], name[50], id_name[50];
    while (fscanf_s(fp, "%s %s %s %s %s",
        id_nicknames, 50,
        phone, 50,
        secret_code, 50,
        name, 50,
        id_name, 50) == 5) {
        User_node* new_node = create_node();
        strcpy_s(new_node->user.id_nicknames, 50, id_nicknames);
 
        strcpy_s(new_node->user.phone, 50, phone);
        
        strcpy_s(new_node->user.secret_code, 50, secret_code);
        
        strcpy_s(new_node->user.name, 50, name);
        
        strcpy_s(new_node->user.id_name, 50, id_name);
        

        append_list(user_list, new_node);
    }

    fclose(fp);
    printf("数据恢复成功！从文件 users_data_backup.txt 恢复 %d 条记录\n", user_list->size);
}
